﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Module;
using System.Data.SqlClient;
using Qmaker.DAL;
using System.Data;

namespace DLL
{
    public class DMenu
    {
        #region SQL语句

        #region 查询

        private const string SQL_SELECT = "SELECT MenuId,MenuName,Path,ParentId,OrderId,Status,DateCreated FROM Menu WHERE Status=1";

        #endregion

        #endregion

        #region 公共接口

        /// <summary>
        /// 获取所有可用的菜单
        /// </summary>
        /// <returns></returns>
        public List<MenuInfo> GetAllEnum()
        {
            List<MenuInfo> result = new List<MenuInfo>();

            MenuInfo menu = null;
            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, SQL_SELECT, null))
            {
                while (reader.Read())
                {
                    menu = GetInstanceByDataReader(reader);
                    if (menu != null)
                        result.Add(menu);
                }
            }
            return result;
        }

        



        #endregion

        #region 私有接口

        private MenuInfo GetInstanceByDataReader(SqlDataReader reader)
        {
            MenuInfo menu = new MenuInfo();
            //MenuId,MenuName,Path,ParentId,OrderId,Status,DateCreated
            menu.MenuId = reader.IsDBNull(0) ? 0 : reader.GetInt32(0);
            menu.MenuName = reader.IsDBNull(1) ? string.Empty : reader.GetString(1);
            menu.Path = reader.IsDBNull(2) ? string.Empty : reader.GetString(2);
            menu.ParentId = reader.IsDBNull(3) ? 0 : reader.GetInt32(3);
            menu.OrderId = reader.IsDBNull(4) ? 0 : reader.GetInt32(4);
            menu.Status = reader.IsDBNull(5) ? 0 : reader.GetInt32(5);
            menu.DateCreated = reader.IsDBNull(6) ? DateTime.MinValue : reader.GetDateTime(6);

            return menu;
        }

        #endregion
    }
}
